<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>改变文件的所有者</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.chmod.html">« chmod</a></li>
      <li style="float: right;"><a href="function.clearstatcache.html">clearstatcache »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>改变文件的所有者</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.chown" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">chown</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">chown</span> &mdash; <span class="dc-title">改变文件的所有者</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.chown-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>chown</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>, <span class="methodparam"><span class="type"><span class="type">string</span>|<span class="type">int</span></span> <code class="parameter">$user</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   尝试将文件 <code class="parameter">filename</code> 的所有者改成用户
   <code class="parameter">user</code>（由用户名或用户 ID 指定）。
   只有超级用户可以改变文件的所有者。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.chown-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">filename</code></dt>

     <dd>

      <p class="para">
       文件路径。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">user</code></dt>

     <dd>

      <p class="para">
       用户名或数字。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.chown-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.chown-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2140">
    <p><strong>示例 #1 简单的 <span class="function"><strong>chown()</strong></span> 用法</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;要使用的文件名和用户名<br /></span><span style="color: #0000BB">$file_name</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"foo.php"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$path&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/home/sites/php.net/public_html/sandbox/"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$file_name&nbsp;</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"root"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;设置用户<br /></span><span style="color: #0000BB">chown</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user_name</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;检测结果<br /></span><span style="color: #0000BB">$stat&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stat</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">posix_getpwuid</span><span style="color: #007700">(</span><span style="color: #0000BB">$stat</span><span style="color: #007700">[</span><span style="color: #DD0000">'uid'</span><span style="color: #007700">]));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [name] =&gt; root
    [passwd] =&gt; x
    [uid] =&gt; 0
    [gid] =&gt; 0
    [gecos] =&gt; root
    [dir] =&gt; /root
    [shell] =&gt; /bin/bash
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.chown-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数不能作用于<a href="features.remote-files.html" class="link">远程文件</a>，被检查的文件必须是可通过服务器的文件系统访问的。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    在 Windows 上对普通文件使用此函数会静默失败。
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.chown-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.chmod.html" class="function" rel="rdfs-seeAlso">chmod()</a> - 改变文件模式</span></li>
    <li class="member"><span class="function"><a href="function.chgrp.html" class="function" rel="rdfs-seeAlso">chgrp()</a> - 改变文件所属的组</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>